package com.yunqiao.api.controller;

import com.yunqiao.api.annotation.Login;
import com.yunqiao.common.exception.CustomApiException;
import com.yunqiao.common.utils.ApiResponse;
import com.yunqiao.common.utils.PageUtils;
import com.yunqiao.service.api.ApplyWithdrawService;
import com.yunqiao.service.api.UserService;
import com.yunqiao.service.api.entity.ApplyWithdrawEntity;
import com.yunqiao.service.api.entity.UserEntity;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Map;


/**
 * 提现申请
 * @author Crocutax
 * @email wangxw725@163.com
 * @date 2019-03-03 21:58:45
 */
@RestController
@RequestMapping("api/applywithdraw")
@Api(tags = "ApplyWithdrawApiController" ,description = "提现申请管理")
public class ApplyWithdrawApiController {

    @Autowired
    private ApplyWithdrawService applyWithdrawService;
    @Autowired
    private UserService userService;

    /**
     * 列表
     */
    @ApiImplicitParams({
        @ApiImplicitParam(name = "status", value = "1待处理 2已处理", required = true, defaultValue = "1", allowableValues = "1,2", paramType = "query", dataType = "int"),
        @ApiImplicitParam(name = "page", value = "页码", required = false ,defaultValue = "1",paramType = "query" , dataType = "int"),
        @ApiImplicitParam(name = "limit", value = "数量", required = false,defaultValue = "10",paramType = "query" , dataType = "int")
    })
    @GetMapping("/list")
    @ApiOperation("查询提现申请 列表")
    public ApiResponse<PageUtils<ApplyWithdrawEntity>> list(@ApiIgnore @RequestParam Map<String, Object> params){
        PageUtils page = applyWithdrawService.queryPage(params);
        return new ApiResponse<>(page);
    }

    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    @ApiOperation("查询单个提现申请")
    public ApiResponse<ApplyWithdrawEntity> info(@PathVariable("id") Long id){
        if (id == null) {
            throw new CustomApiException("ID不能为空");
        }
        ApplyWithdrawEntity applyWithdraw = applyWithdrawService.getById(id);
        return new ApiResponse<>(applyWithdraw);
    }

    /**
     * 保存
     */
    @Login
    @PostMapping("/save")
    @ApiOperation("保存提现申请")
    public ApiResponse save(@RequestBody ApplyWithdrawEntity applyWithdraw){
        UserEntity userEntity = userService.getById(applyWithdraw.getUserId());
        if (applyWithdraw.getAmount().doubleValue() > userEntity.getBalance().doubleValue()) {
            throw new CustomApiException("你的账户余额不足");
        }
        applyWithdrawService.apply(applyWithdraw);
        return new ApiResponse<>();
    }

    /**
     * 修改
     */
    @Login
    @PutMapping("/update/{id}")
    @ApiOperation("修改提现申请")
    public ApiResponse update(@PathVariable Long id , ApplyWithdrawEntity applyWithdraw){
        ApplyWithdrawEntity dbItem = applyWithdrawService.getById(id);
        if(dbItem==null){
            throw new CustomApiException("ID有误, 未找到对应数据");
        }
        applyWithdrawService.updateById(applyWithdraw);
        return new ApiResponse<>();
    }

    /**
     * 删除
     */
    @Login
    @DeleteMapping("/delete/{id}")
    @ApiOperation("删除提现申请")
    public ApiResponse delete(@PathVariable Long id){
        if (id == null) {
            throw new CustomApiException("ID不能为空");
        }
        applyWithdrawService.removeById(id);
        return new ApiResponse<>();
    }

    @ApiImplicitParams({
        @ApiImplicitParam(name = "userId", value = "用户id", required = true, paramType = "query", dataType = "Long"),
        @ApiImplicitParam(name = "amount", value = "提现金额", required = true, paramType = "query", dataType = "Double")
    })
    @Login
    @DeleteMapping("/apply")
    @ApiOperation("提现申请")
    public ApiResponse apply(@RequestBody ApplyWithdrawEntity applyWithdrawEntity){
        applyWithdrawService.apply(applyWithdrawEntity);
        return new ApiResponse<>();
    }
}
